
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\match_victim_data_clean_new", clear

g shnro_perp = sphnro0

local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse same_munic new_child
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}
g same_munic0 = (kunta0 == kuntaB1)
forvalues i = 1/5 {
	g same_municB`i' = (kunta0 == kuntaB`i')
}
forvalues i = 1/5 {
	g same_municF`i' = (kunta0 == kuntaF`i')
}

g new_child0 = (nchild0<nchildB1)
forvalues i = 1/5 {
	g new_childB`i' = (nchild0 < nchildB`i')
}

forvalues i = 1/5 {
	g new_childF`i' = (nchild0 < nchildF`i')
}


forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n

reshape long `varlist', i(group_id) j(time)

// add in victim svatva 
g time_ = time-6
g cal_year = year_start_cohab + time_
g svatva_ = . 
g ytmp = .
forvalues i = 2001/2016 {
    replace ytmp = `i'
		merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`i'.dta", ///
				keepusing(svatva)
			drop if _merge==2
			replace svatva_ = svatva if cal_year == ytmp & _merge==3
			drop _merge svatva 
}
drop year 
ren cal_year year 
merge m:1 shnro year using "$data_folder_final\FOLK_tyotu_2017_2019.dta"
drop if _merge ==2
replace svatva_ = svatva if year >= 2017 & _merge==3
drop svatva ytmp _merge 
ren svatva_ svatva_victim

// add in perp svatva 
ren shnro shnro_victim
ren shnro_perp shnro
ren tyotu tyotu_victim
g svatva_ = . 
g perp_earn = .
g ytmp = .

g cal_year = year_start_cohab + time_
forvalues i = 2001/2016 {
    replace ytmp = `i'
		merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`i'.dta", ///
				keepusing(svatva tyotu)
			drop if _merge==2
			replace svatva_ = svatva if cal_year == ytmp & _merge==3
			replace perp_earn = tyotu 
			drop _merge svatva tyotu
}
drop year 
ren cal_year year 
merge m:1 shnro year using "$data_folder_final\FOLK_tyotu_2017_2019.dta"
drop if _merge ==2
replace svatva_ = svatva if year >= 2017 & _merge==3
replace perp_earn = tyotu if year >=2017 & _merge==3
drop svatva tyotu ytmp _merge 
ren svatva_ svatva_perp
ren perp_earn tyotu_perp
ren tyotu_victim tyotu
ren shnro shnro_perp
ren shnro_victim shnro 

merge m:1 year using "W:\intimate\data\cpi.dta"
keep if _merge==3
drop _merge
replace svatva_victim = svatva_victim/cpi
replace svatva_perp = svatva_perp/cpi
replace tyotu_perp = tyotu_perp/cpi
replace tyotu_perp = 0 if missing(tyotu_perp)
replace tyotu_perp = . if info_missing ==1
replace tyotu_perp =. if year==2019
replace tyotu =. if year==2019

g total_income = tyotu_perp + tyotu

*Define dummies used in the event studies
drop year year_event
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat= dv_couple==1

*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"

capture program drop eventStudyGraphs_victim
program define eventStudyGraphs_victim
	args a b c 
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies, absorb($fe)  cluster($cluster)	
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i']  if t == -`i'
		cap	replace se_est =  _se[dpl_`i']  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i']  if t == `i'
		cap	replace se_est =  _se[dpl`i']  if t == `i'
	}

	replace coef_est = 0 if t == - 1
	replace se_est = 0 if t == -1
	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	
	*Main regression 
	reghdfe `a'  treatPost,  absorb($fe)  cluster($cluster)
	local beta = string(_b[treatPost], "%10.3fc")
	local se = string(_se[treatPost], "%10.3fc")
	gen obs = e(N)
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time from Cohabitation") ), ///
		 graphregion(color(white)) legend(off)
	graph export "$result_folder\victim_match_`a'.pdf", replace
	restore
	}
end

g total_svatva = svatva_victim + svatva_perp


eventStudyGraphs_victim "svatva_victim" "-2500(500)1000" "Earnings + Transfer" 


preserve 
keep svatva_victim total_svatva time dv_couple
collapse (mean) total_svatva svatva_victim, by(time dv_couple)

// Base 
g dev_ts = total_svatva 
replace dev_ts = total_svatva - 39842.06 if dv_couple==1
replace dev_ts = total_svatva - 40228.24 if dv_couple==0

graph twoway (line dev_ts time if dv_couple==1, lcolor(black)) ///
		(line dev_ts time if dv_couple==0, lcolor(red)), ///
		xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5") ///
		xtitle("Time from Cohabitation") ytitle("Total Income + Transfers (diff from -1)") ///
		graphregion(color(white)) ///
		xline(6, lpattern(dash) lcolor(black)) ///
		legend(order(1 "DV Couple" 2 "Non-DV Couple"))
graph export "$result_folder\totalsvatva_descrip_base.pdf", replace


restore

preserve 
keep total_income time dv_couple
collapse (mean) total_income, by(time dv_couple)

//Base
g dev_ts = total_income 
replace dev_ts = total_income - 31751.03 if dv_couple==1
replace dev_ts = total_income - 36327.7 if dv_couple==0

graph twoway (line dev_ts time if dv_couple==1, lcolor(black)) ///
		(line dev_ts time if dv_couple==0, lcolor(red)), ///
		xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5") ///
		xtitle("Time from Cohabitation") ytitle("Total Income (diff from -1)") ///
		graphregion(color(white)) ///
		xline(6, lpattern(dash) lcolor(black)) ///
		legend(order(1 "DV Couple" 2 "Non-DV Couple"))
graph export "$result_folder\totalincome_descrip_base.pdf", replace

restore
